﻿using GameFramework.Event;
using System;
using System.Collections;
using System.Collections.Generic;
using TowerDefence_Zombie.Data;
using UnityEngine;

namespace TowerDefence_Zombie
{
    public class GuideCondition_CloseUI : GuideCondition_Base
    {
        private UIData uIData;

        public GuideCondition_CloseUI(DRGuideCondition conditionInfo, Action conitionCompleteClick)
            : base(conditionInfo, conitionCompleteClick)
        {
            int uiId = conditionInfo.ConditionParams_Int;
            uIData = GameEntry.Data.GetData<DataUI>().GetUIData(uiId);
        }

        public override void StartCondition()
        {
            base.StartCondition();
            GameEntry.Event.Subscribe(OpenUIEventArgs.EventId, OnUIOpen);
            GameEntry.Event.Subscribe(CloseUIEventArgs.EventId, OnUIClose);

            var uiForm = GameEntry.UI.GetUIForm(uIData.AssetPath);
            if (uiForm == null || uiForm.Logic.Visible == false)
            {
                IsCompleteFlag = true;
            }
        }

        private void OnUIOpen(object sender, GameEventArgs e)
        {
            OpenUIEventArgs ne = (OpenUIEventArgs)e;
            if (ne == null)
                return;

            if (ne.UIAssetsPath == uIData.AssetPath)
            {
                IsCompleteFlag = false;
            }
        }

        private void OnUIClose(object sender, GameEventArgs e)
        {
            CloseUIEventArgs ne = (CloseUIEventArgs)e;
            if (ne == null)
                return;

            if (ne.UIAssetsPath == uIData.AssetPath)
            {
                IsCompleteFlag = true;
            }
        }

        public override void Dispose()
        {
            GameEntry.Event.Unsubscribe(OpenUIEventArgs.EventId, OnUIOpen);
            GameEntry.Event.Unsubscribe(CloseUIEventArgs.EventId, OnUIClose);
            base.Dispose();
        }
    }
}
